﻿using Configuracion.Models;
using ConfigurationServer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace ConfigurationServer.AppCode.Utilities
{
    public class CTimeTrack
    {
        private DateTime relativeStart;
        public DateTime absoluteStart;
        public Dictionary<string, double> tracks;

        public CTimeTrack()
        {
            inicio();
        }
        public void inicio()
        {
            absoluteStart = DateTime.Now;
            relativeStart = absoluteStart;
            tracks = new Dictionary<string, double>();
        }
        public void timeTrack(string strLog)
        {
            tracks.Add(strLog, (DateTime.Now - relativeStart).TotalMilliseconds);
            relativeStart = DateTime.Now;
        }
        public async Task saveAsync(string controller, CTimeTrack timeTrack)
        {
            mdlTimeTrackCall call = new mdlTimeTrackCall(controller, timeTrack);
            
             dataContext dbContext = new dataContext();

             dbContext.timeTrackCalls.Add(call);

            if (dbContext.GetValidationErrors().Count()== 0)
                await dbContext.SaveChangesAsync();

            // it if fails ...
        }
    }
}